package com.yanqu.road.server.manger;

import com.yanqu.road.entity.config.vip.VipInfo;
import com.yanqu.road.logic.bussiness.config.VipBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.Map;

/**
 * vip信息管理
 */
public class VipMgr extends TempMgr {

    private static Map<Integer, VipInfo> vipInfoMap;

    private static int maxVipLv;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        return false;
    }

    @Override
    public boolean reloadConfig() {
        vipInfoMap = VipBussiness.getVipInfoMap();
        if(null == vipInfoMap || vipInfoMap.size() <= 0){
            return false;
        }
        int max = 0;
        for(VipInfo vipInfo : vipInfoMap.values()){
            if(vipInfo.getLevel() > max){
                max = vipInfo.getLevel();
            }
        }
        maxVipLv = max;
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static VipInfo getVipInfo(int vipLv){
        return vipInfoMap.get(vipLv);
    }

    public static VipInfo getVipInfo(long vipExp){
        int vipLv = 0;
        if(null == vipInfoMap) {
            return null;
        }
        for(int i = 0; i < vipInfoMap.size(); i++){
            VipInfo vipInfo = vipInfoMap.get(i);
            if(vipExp >= vipInfo.getVipExp()){
                vipLv = vipInfo.getLevel() + 1;
            }else {
                break;
            }
        }
        if(vipLv > maxVipLv){
            vipLv = maxVipLv;
        }
        return getVipInfo(vipLv);
    }

    public static int getVipLv(long vipExp){
        VipInfo vipInfo = getVipInfo(vipExp);
        if(null != vipInfo){
            return vipInfo.getLevel();
        }
        return 0;
    }

    public static Map<Integer, VipInfo> getVipInfoMap() {
        return vipInfoMap;
    }

    /**
     * 获取vip等级
     * @return
     */
    public static int getMaxVipLv() {
        return maxVipLv;
    }
}
